<div id="cnblogs_post_body" class="blogpost-body">
	<h1 id="quartus-入门--fpga-超声波测距hc-sr04" tid="tid-p4ZBG2">Quartus 入门 —— FPGA 超声波测距（HC-SR04）<button class="cnblogs-toc-button" title="显示目录导航" aria-expanded="false"></button></h1>
	<p>这里我们跳过项目创建以及代码导入的过程，直接介绍项目核心内容的设计</p>
	<h2 id="模块设计" tid="tid-wmnssx">模块设计<button class="cnblogs-toc-button" title="显示目录导航" aria-expanded="false"></button></h2>
	<div align="center">
	<a data-fancybox="gallery" href="https://img2023.cnblogs.com/blog/2829072/202305/2829072-20230520170847516-632935113.png"><img src="https://img2023.cnblogs.com/blog/2829072/202305/2829072-20230520170847516-632935113.png"></a>
	</div>
	<p>根据波形本项目总共分为五个模块：</p>
	<ul>
	<li>clk_div：计时模块：通过计时给出需要输出电平、数吗管段选位移或进行数据处理的信号</li>
	</ul>
	<p>这里只需要设计 1MHz 的时钟即可：</p>
	<code-box id="code-4mkkGw"><button boxid="code-4mkkGw" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-4mkkGw pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-4mkkGw" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">parameter</span> CNT_MAX = <span class="hljs-number">19'd49</span>;<span class="hljs-comment">//1us的计数值为 50 * Tclk（20ns）</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">5</span>:<span class="hljs-number">0</span>]	cnt		; 
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span>			add_cnt ;
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span>			end_cnt ;
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">6'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(cnt == CNT_MAX) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">6'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= cnt + <span class="hljs-number">6'd1</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> clk_us = cnt &gt;= CNT_MAX ;</pre></code-box>
	<ul>
	<li>trig_driver：主要负责控制 10μS 持续电平输出，按照系统设计，平均每 1s 先等待 10μS，信号稳定后输出 10μS 持续电平</li>
	</ul>
	<p>这里主要进行判断，判断trig何时需要输出，根据时序图判断，只有在 1MHz 时钟下，平均每 100μS 输出10μS 高电平，就可以驱动超声波</p>
	<code-box id="code-ykWFfZ"><button boxid="code-ykWFfZ" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-ykWFfZ pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-ykWFfZ" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk_us <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">19'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(cnt == CYCLE_MAX) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">19'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= cnt + <span class="hljs-number">19'd1</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> trig = cnt &lt; <span class="hljs-number">15</span> ? <span class="hljs-number">1'b1</span> : <span class="hljs-number">1'b0</span>;</pre></code-box>
	<ul>
	<li>echo_triger：输入信号处理，回响电平输出与检测距离正比，通过计算输出距离数据</li>
	</ul>
	<p>首先我们需要进行边缘检测，检测上升沿和下降沿：</p>
	<code-box id="code-f5Cx5B"><button boxid="code-f5Cx5B" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-f5Cx5B pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-f5Cx5B" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">parameter</span> T_MAX = <span class="hljs-number">16'd60_000</span>;<span class="hljs-comment">//510cm 对应计数值</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>				r1_echo,r2_echo; <span class="hljs-comment">//边沿检测</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span>			echo_pos,echo_neg; <span class="hljs-comment">//</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-comment">//如果使用clk_us 检测边沿，延时2us，差值过大</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn)<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn)<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			r1_echo &lt;= <span class="hljs-number">1'b0</span>;
	<code-line class="line-numbers-rows"></code-line>			r2_echo &lt;= <span class="hljs-number">1'b0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			r1_echo &lt;= echo;
	<code-line class="line-numbers-rows"></code-line>			r2_echo &lt;= r1_echo;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>  
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> echo_pos = r1_echo &amp; ~r2_echo;
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> echo_neg = ~r1_echo &amp; r2_echo;
	<code-line class="line-numbers-rows"></code-line></pre></code-box>
	<p>然后我们需要对收到的距离信息进行处理，得出我们想要的数据，这里我们通过将数据 *1000 来处理小数</p>
	<code-box id="code-8MGzbt"><button boxid="code-8MGzbt" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-8MGzbt pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-8MGzbt" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn)<span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn)<span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>			data_r &lt;= <span class="hljs-number">'d2</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>  
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(echo_neg)<span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>			data_r &lt;= (cnt &lt;&lt; <span class="hljs-number">4</span>) + cnt;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>  
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>			data_r &lt;= data_r;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>  
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> data_o = data_r &gt;&gt; <span class="hljs-number">1</span>;</pre></code-box>
	<ul>
	<li>seg_driver：计算得到数码管需要输出的内容并实时显示</li>
	</ul>
	<p>计算每一位的输出数据：</p>
	<code-box id="code-TYkseM"><button boxid="code-TYkseM" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-TYkseM pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-TYkseM" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn)<span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn)<span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>			cm_hund	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_ten	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_unit	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			point_1	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			point_2	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			point_3	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cm_hund &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">5</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_ten	&lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">4</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_unit &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">3</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			point_1 &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">2</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			point_2 &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">1</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			point_3 &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">0</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span></pre></code-box>
	<p>这里我们通过函数判断输出的数据：</p>
	<code-box id="code-ZzFEtk"><button boxid="code-ZzFEtk" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-ZzFEtk pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-ZzFEtk" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line>    <span class="hljs-comment">// 函数，4位输入，7位输出，判断要输出的数字</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">function</span>  [<span class="hljs-number">7</span>:<span class="hljs-number">0</span>]	hex_data; <span class="hljs-comment">//函数不含时序逻辑相关</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">input</span>   [<span class="hljs-number">03</span>:<span class="hljs-number">00</span>]	data_i;<span class="hljs-comment">//至少一个输入</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">case</span>(data_i)
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd0</span>:hex_data = NUM_0;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd1</span>:hex_data = NUM_1;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd2</span>:hex_data = NUM_2;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd3</span>:hex_data = NUM_3;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd4</span>:hex_data = NUM_4;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd5</span>:hex_data = NUM_5;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd6</span>:hex_data = NUM_6;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd7</span>:hex_data = NUM_7;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd8</span>:hex_data = NUM_8;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd9</span>:hex_data = NUM_9;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-keyword">default</span>:hex_data = ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">endcase</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">endfunction</span></pre></code-box>
	<ul>
	<li>HC_SR04_TOP：将上述模块连接起来，作为系统的顶层模块</li>
	</ul>
	<h2 id="代码设计" tid="tid-j3hkXm">代码设计<button class="cnblogs-toc-button" title="显示目录导航" aria-expanded="false"></button></h2>
	<p>clk_div.v：</p>
	<code-box id="code-syP3kG"><button boxid="code-syP3kG" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-syP3kG pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-syP3kG" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">module</span> 	clk_div(
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  <span class="hljs-keyword">wire</span>			clk		,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  <span class="hljs-keyword">wire</span> 		rstn	,
	<code-line class="line-numbers-rows"></code-line>		   
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span> <span class="hljs-keyword">wire</span>  		clk_us 	  <span class="hljs-comment">//</span>
	<code-line class="line-numbers-rows"></code-line>);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">parameter</span> CNT_MAX = <span class="hljs-number">19'd49</span>;<span class="hljs-comment">//1us的计数值为 50 * Tclk（20ns）</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">5</span>:<span class="hljs-number">0</span>]	cnt		;
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span>			add_cnt ;
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span>			end_cnt ;
	<code-line class="line-numbers-rows"></code-line>	
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-comment">// 时钟分频</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">6'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(cnt == CNT_MAX) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">6'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= cnt + <span class="hljs-number">6'd1</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> clk_us = cnt &gt;= CNT_MAX ;
	<code-line class="line-numbers-rows"></code-line>	
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">endmodule</span></pre></code-box>
	<p>trig_driver.v：</p>
	<code-box id="code-EA4RkN"><button boxid="code-EA4RkN" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-EA4RkN pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-EA4RkN" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">module</span> 	trig_driver(
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  <span class="hljs-keyword">wire</span>			clk_us	,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  <span class="hljs-keyword">wire</span> 		rstn	,
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span> <span class="hljs-keyword">wire</span>  		trig	  <span class="hljs-comment">//触发测距信号</span>
	<code-line class="line-numbers-rows"></code-line>);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">parameter</span> CYCLE_MAX = <span class="hljs-number">19'd29_9999</span>;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">18</span>:<span class="hljs-number">00</span>]	cnt		;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment">// 10毫秒持续电平输出</span>
	<code-line class="line-numbers-rows"></code-line>	
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk_us <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">19'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(cnt == CYCLE_MAX) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">19'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= cnt + <span class="hljs-number">19'd1</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> trig = cnt &lt; <span class="hljs-number">15</span> ? <span class="hljs-number">1'b1</span> : <span class="hljs-number">1'b0</span>;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">endmodule</span></pre></code-box>
	<p>echo_triger.v：</p>
	<code-box id="code-EKXmhr"><button boxid="code-EKXmhr" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-EKXmhr pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-EKXmhr" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">module</span> 	echo_driver(
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  <span class="hljs-keyword">wire</span> 		clk		,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  <span class="hljs-keyword">wire</span>			clk_us	,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  <span class="hljs-keyword">wire</span> 		rstn	,
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  <span class="hljs-keyword">wire</span> 		echo	,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span> <span class="hljs-keyword">wire</span> [<span class="hljs-number">18</span>:<span class="hljs-number">00</span>]	data_o	  <span class="hljs-comment">//检测距离，保留3位小数，*1000实现</span>
	<code-line class="line-numbers-rows"></code-line>);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">parameter</span> T_MAX = <span class="hljs-number">16'd5_9999</span>;<span class="hljs-comment">//510cm 对应计数值</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>				r1_echo,r2_echo; <span class="hljs-comment">//边沿检测</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span>			echo_pos,echo_neg;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">15</span>:<span class="hljs-number">00</span>]	cnt		;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">18</span>:<span class="hljs-number">00</span>]	data_r	;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-comment">//如果使用clk_us 检测边沿，延时2us，差值过大</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn)<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn)<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			r1_echo &lt;= <span class="hljs-number">1'b0</span>;
	<code-line class="line-numbers-rows"></code-line>			r2_echo &lt;= <span class="hljs-number">1'b0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			r1_echo &lt;= echo;
	<code-line class="line-numbers-rows"></code-line>			r2_echo &lt;= r1_echo;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> echo_pos = r1_echo &amp; ~r2_echo;
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> echo_neg = ~r1_echo &amp; r2_echo;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk_us <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">16'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(echo) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">if</span>(cnt == T_MAX) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>				cnt &lt;= <span class="hljs-number">16'd0</span>;
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>				cnt &lt;= cnt + <span class="hljs-number">16'd1</span>;
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt &lt;= <span class="hljs-number">16'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn)<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn)<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			data_r &lt;= <span class="hljs-number">'d2</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(echo_neg)<span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>			data_r &lt;= (cnt &lt;&lt; <span class="hljs-number">4</span>) + cnt;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			data_r &lt;= data_r;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">assign</span> data_o = data_r &gt;&gt; <span class="hljs-number">1</span>;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">endmodule</span> 
	<code-line class="line-numbers-rows"></code-line></pre></code-box>
	<p>seg_driver.v：</p>
	<code-box id="code-tswxi3"><button boxid="code-tswxi3" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-tswxi3 pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-tswxi3" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">module</span> seg_driver(
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>	<span class="hljs-keyword">wire</span>		clk		,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>	<span class="hljs-keyword">wire</span>		rstn	,
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>	<span class="hljs-keyword">wire</span> [<span class="hljs-number">18</span>:<span class="hljs-number">0</span>]	data_in	, <span class="hljs-comment">//待显示数据</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>  <span class="hljs-keyword">reg</span> [<span class="hljs-number">7</span>:<span class="hljs-number">0</span>]   sel     ,	<span class="hljs-comment">// 我这里是8位段选，可以换6位，但是要自己改代码</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>  <span class="hljs-keyword">reg</span> [<span class="hljs-number">7</span>:<span class="hljs-number">0</span>]   seg
	<code-line class="line-numbers-rows"></code-line>);
	<code-line class="line-numbers-rows"></code-line> 	<span class="hljs-comment">//parameter define</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">localparam</span>	NUM_0	=	<span class="hljs-number">8'b1100_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_1 	= 	<span class="hljs-number">8'b1111_1001</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_2   = 	<span class="hljs-number">8'b1010_0100</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_3   = 	<span class="hljs-number">8'b1011_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_4   = 	<span class="hljs-number">8'b1001_1001</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_5   = 	<span class="hljs-number">8'b1001_0010</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_6   = 	<span class="hljs-number">8'b1000_0010</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_7   = 	<span class="hljs-number">8'b1111_1000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_8   = 	<span class="hljs-number">8'b1000_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_9   = 	<span class="hljs-number">8'b1001_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_A   = 	<span class="hljs-number">8'b1000_1000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_B   = 	<span class="hljs-number">8'b1000_0011</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_C   = 	<span class="hljs-number">8'b1100_0110</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_D   = 	<span class="hljs-number">8'b1010_0001</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_E   = 	<span class="hljs-number">8'b1000_0110</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_F   = 	<span class="hljs-number">8'b1000_1110</span>,
	<code-line class="line-numbers-rows"></code-line>				ALL_LIGHT = <span class="hljs-number">8'b0000_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				LIT_OUT = 	<span class="hljs-number">8'b1111_1111</span>,
	<code-line class="line-numbers-rows"></code-line>                LINE    =   <span class="hljs-number">8'b1011_1111</span>;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">localparam</span> MAX_10us     =   <span class="hljs-number">10'd999</span>     ;
	<code-line class="line-numbers-rows"></code-line> 	<span class="hljs-comment">//reg 、wire define</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	cm_hund	    ;<span class="hljs-comment">//100cm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	cm_ten	    ;<span class="hljs-comment">//10cm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	cm_unit	    ;<span class="hljs-comment">//1cm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	point_1	    ;<span class="hljs-comment">//1mm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	point_2	    ;<span class="hljs-comment">//0.1mm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	point_3	    ;<span class="hljs-comment">//0.01mm</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">reg</span>     [<span class="hljs-number">9</span>:<span class="hljs-number">0</span>]   cnt_10us    ;
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">reg</span>     [<span class="hljs-number">7</span>:<span class="hljs-number">0</span>]   num         ;<span class="hljs-comment">// 段选输出判断</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn)<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn)<span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>			cm_hund	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_ten	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_unit	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			point_1	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			point_2	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			point_3	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>  
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>			cm_hund &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">5</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_ten	&lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">4</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_unit &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">3</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			point_1 &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">2</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			point_2 &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">1</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			point_3 &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">0</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>  
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span> 
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-comment">// 修改后   段选</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt_10us &lt;= <span class="hljs-number">10'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(cnt_10us == MAX_10us) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt_10us &lt;= <span class="hljs-number">10'd0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cnt_10us &lt;= cnt_10us + <span class="hljs-number">10'd1</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-comment">// 数码管位移</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">if</span>(!rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>            sel &lt;= <span class="hljs-number">8'b1000_0000</span>;
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(cnt_10us == MAX_10us) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>            sel &lt;= {sel[<span class="hljs-number">0</span>],sel[<span class="hljs-number">7</span>:<span class="hljs-number">1</span>]};
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>            sel &lt;= sel;
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-comment">// 确定输出数字</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">always</span> @(*) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">case</span> (sel)
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-number">8'b0000_0001</span>    :    num = hex_data(point_3);
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-number">8'b0000_0010</span>    :    num = hex_data(point_2);
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-number">8'b0000_0100</span>    :    num = hex_data(point_1);
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-number">8'b0000_1000</span>    :    num = LINE;
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-number">8'b0001_0000</span>    :    num = hex_data(cm_unit);
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-number">8'b0010_0000</span>    :    num = hex_data(cm_ten);
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-number">8'b0100_0000</span>    :    num = hex_data(cm_hund);
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-number">8'b1000_0000</span>    :    num = LIT_OUT;
	<code-line class="line-numbers-rows"></code-line>            <span class="hljs-keyword">default</span>         :    num = NUM_0;
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">endcase</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-comment">// 位选输出</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">if</span>(!rstn) <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>            seg &lt;= LINE;
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>            <span class="hljs-keyword">case</span> (num)
	<code-line class="line-numbers-rows"></code-line>                NUM_0       :   seg &lt;= NUM_0    ;
	<code-line class="line-numbers-rows"></code-line>                NUM_1       :   seg &lt;= NUM_1    ;
	<code-line class="line-numbers-rows"></code-line>                NUM_2       :   seg &lt;= NUM_2    ;
	<code-line class="line-numbers-rows"></code-line>                NUM_3       :   seg &lt;= NUM_3    ;
	<code-line class="line-numbers-rows"></code-line>                NUM_4       :   seg &lt;= NUM_4    ;
	<code-line class="line-numbers-rows"></code-line>                NUM_5       :   seg &lt;= NUM_5    ;
	<code-line class="line-numbers-rows"></code-line>                NUM_6       :   seg &lt;= NUM_6    ;
	<code-line class="line-numbers-rows"></code-line>                NUM_7       :   seg &lt;= NUM_7    ;
	<code-line class="line-numbers-rows"></code-line>                NUM_8       :   seg &lt;= NUM_8    ;
	<code-line class="line-numbers-rows"></code-line>                NUM_9       :   seg &lt;= NUM_9    ;
	<code-line class="line-numbers-rows"></code-line>                LINE        :   seg &lt;= LINE     ;
	<code-line class="line-numbers-rows"></code-line>                LIT_OUT     :   seg &lt;= LIT_OUT  ;
	<code-line class="line-numbers-rows"></code-line>                ALL_LIGHT   :   seg &lt;= ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>            <span class="hljs-keyword">endcase</span>
	<code-line class="line-numbers-rows"></code-line>        <span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-comment">// 函数，4位输入，7位输出，判断要输出的数字</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">function</span>  [<span class="hljs-number">7</span>:<span class="hljs-number">0</span>]	hex_data; <span class="hljs-comment">//函数不含时序逻辑相关</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">input</span>   [<span class="hljs-number">03</span>:<span class="hljs-number">00</span>]	data_i;<span class="hljs-comment">//至少一个输入</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">case</span>(data_i)
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd0</span>:hex_data = NUM_0;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd1</span>:hex_data = NUM_1;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd2</span>:hex_data = NUM_2;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd3</span>:hex_data = NUM_3;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd4</span>:hex_data = NUM_4;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd5</span>:hex_data = NUM_5;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd6</span>:hex_data = NUM_6;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd7</span>:hex_data = NUM_7;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd8</span>:hex_data = NUM_8;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">4'd9</span>:hex_data = NUM_9;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-keyword">default</span>:hex_data = ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">endcase</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">endfunction</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">endmodule</span></pre></code-box>
	<p>HC_SR04_TOP.v：</p>
	<code-box id="code-8Zxj5M"><button boxid="code-8Zxj5M" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-8Zxj5M pre" aria-label="复制代码"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-8Zxj5M" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">module</span> 	HC_SR04_TOP(
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  			clk		,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>   		rstn	,
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>   		echo	, <span class="hljs-comment">// 距离信号</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span>   		trig	, <span class="hljs-comment">// 触发测距信号</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span>  <span class="hljs-keyword">wire</span> [<span class="hljs-number">7</span>:<span class="hljs-number">0</span>]	sel	,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span>  <span class="hljs-keyword">wire</span> [<span class="hljs-number">7</span>:<span class="hljs-number">0</span>]	seg
	<code-line class="line-numbers-rows"></code-line>);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span>	[<span class="hljs-number">18</span>:<span class="hljs-number">00</span>]		data_o		;
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span> 				clk_us		;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	seg_driver u_seg_driver(
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk</span>		(clk	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.rstn</span>		(rstn	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.data_in</span>	(data_o	), <span class="hljs-comment">//待显示数据</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.sel</span>		(sel	),	<span class="hljs-comment">// 我这里是8位段选，可以换6位，但是要自己改代码</span>
	<code-line class="line-numbers-rows"></code-line>    	<span class="hljs-variable">.seg</span>		(seg	)
	<code-line class="line-numbers-rows"></code-line>	);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	clk_div	u_clk_div(
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk</span>		(clk	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.rstn</span>		(rstn	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk_us</span>		(clk_us )
	<code-line class="line-numbers-rows"></code-line>	);
	<code-line class="line-numbers-rows"></code-line>	trig_driver	u_trig_driver(
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk_us</span>		(clk_us	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.rstn</span>		(rstn	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.trig</span>		(trig	)
	<code-line class="line-numbers-rows"></code-line>	);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	echo_driver	u_echo_driver(
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk</span>		(clk	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk_us</span>		(clk_us	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.rstn</span>		(rstn	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.echo</span>		(echo	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.data_o</span>		(data_o	)
	<code-line class="line-numbers-rows"></code-line>		);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">endmodule</span> 
	<code-line class="line-numbers-rows"></code-line></pre></code-box>
	<p>如果使用 DE2-115 开发板，由于芯片资源多，数码管没有段选，只有位选，所以这里我们采用如下代码：</p>
	<p>seg_driver.v：</p>
	<code-box id="code-ipj8p3"><button boxid="code-ipj8p3" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-ipj8p3 pre" aria-label="复制代码"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-ipj8p3" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">module</span> seg_driver(
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>	<span class="hljs-keyword">wire</span>		clk		,  <span class="hljs-comment">//50MHz</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>	<span class="hljs-keyword">wire</span>		rst_n	,  <span class="hljs-comment">//low valid</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>	<span class="hljs-keyword">wire</span> [<span class="hljs-number">18</span>:<span class="hljs-number">0</span>]	data_in	, <span class="hljs-comment">//待显示数据</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span>	<span class="hljs-keyword">reg</span>	[<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex1	, <span class="hljs-comment">// -共阳极，低电平有效</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span>	<span class="hljs-keyword">reg</span> [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex2	, <span class="hljs-comment">// -</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>	<span class="hljs-keyword">reg</span> [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex3	, <span class="hljs-comment">// -</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>	<span class="hljs-keyword">reg</span> [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex4	, <span class="hljs-comment">//连接符</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>	<span class="hljs-keyword">reg</span> [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex5	, <span class="hljs-comment">//cm - </span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>	<span class="hljs-keyword">reg</span> [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex6	, <span class="hljs-comment">//cm - </span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>	<span class="hljs-keyword">reg</span> [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex7	, <span class="hljs-comment">//cm - </span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>	<span class="hljs-keyword">reg</span> [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex8	  <span class="hljs-comment">//熄灭</span>
	<code-line class="line-numbers-rows"></code-line>);
	<code-line class="line-numbers-rows"></code-line> 	<span class="hljs-comment">//parameter define</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">localparam</span>	NUM_0	=	<span class="hljs-number">8'b1100_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_1 	= 	<span class="hljs-number">8'b1111_1001</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_2   = 	<span class="hljs-number">8'b1010_0100</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_3   = 	<span class="hljs-number">8'b1011_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_4   = 	<span class="hljs-number">8'b1001_1001</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_5   = 	<span class="hljs-number">8'b1001_0010</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_6   = 	<span class="hljs-number">8'b1000_0010</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_7   = 	<span class="hljs-number">8'b1111_1000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_8   = 	<span class="hljs-number">8'b1000_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_9   = 	<span class="hljs-number">8'b1001_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_A   = 	<span class="hljs-number">8'b1000_1000</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_B   = 	<span class="hljs-number">8'b1000_0011</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_C   = 	<span class="hljs-number">8'b1100_0110</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_D   = 	<span class="hljs-number">8'b1010_0001</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_E   = 	<span class="hljs-number">8'b1000_0110</span>,
	<code-line class="line-numbers-rows"></code-line>				NUM_F   = 	<span class="hljs-number">8'b1000_1110</span>,
	<code-line class="line-numbers-rows"></code-line>				ALL_LIGHT = <span class="hljs-number">8'b0000_0000</span>,
	<code-line class="line-numbers-rows"></code-line>				LIT_OUT = 	<span class="hljs-number">8'b1111_1111</span>;
	<code-line class="line-numbers-rows"></code-line> 	<span class="hljs-comment">//reg 、wire define</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	cm_hund	;<span class="hljs-comment">//100cm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	cm_ten	;<span class="hljs-comment">//10cm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	cm_unit	;<span class="hljs-comment">//1cm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	point_1	;<span class="hljs-comment">//1mm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	point_2	;<span class="hljs-comment">//0.1mm</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">reg</span>		[<span class="hljs-number">3</span>:<span class="hljs-number">0</span>]	point_3	;<span class="hljs-comment">//0.01mm</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rst_n)<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rst_n)<span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>			cm_hund	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_ten	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_unit	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			point_1	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			point_2	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>			point_3	&lt;= <span class="hljs-number">'d0</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			cm_hund &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">5</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_ten	&lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">4</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			cm_unit &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">3</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			point_1 &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">2</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			point_2 &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">1</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>			point_3 &lt;= data_in / <span class="hljs-number">10</span> ** <span class="hljs-number">0</span> % <span class="hljs-number">10</span>;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">always</span> @(<span class="hljs-keyword">posedge</span> clk <span class="hljs-keyword">or</span> <span class="hljs-keyword">negedge</span> rst_n)<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">if</span>(!rst_n)<span class="hljs-keyword">begin</span>  
	<code-line class="line-numbers-rows"></code-line>			hex1 &lt;= ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			hex2 &lt;= ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			hex3 &lt;= ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			hex4 &lt;= ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			hex5 &lt;= ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			hex6 &lt;= ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			hex7 &lt;= ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			hex8 &lt;= ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			hex1 &lt;= hex_data(point_3);
	<code-line class="line-numbers-rows"></code-line>			hex2 &lt;= hex_data(point_2);
	<code-line class="line-numbers-rows"></code-line>			hex3 &lt;= hex_data(point_1);
	<code-line class="line-numbers-rows"></code-line>			hex4 &lt;= <span class="hljs-number">7'b011_1111</span>;
	<code-line class="line-numbers-rows"></code-line>			hex5 &lt;= hex_data(cm_unit);
	<code-line class="line-numbers-rows"></code-line>			hex6 &lt;= hex_data(cm_ten);
	<code-line class="line-numbers-rows"></code-line>			hex7 &lt;= hex_data(cm_hund);
	<code-line class="line-numbers-rows"></code-line>			hex8 &lt;= LIT_OUT;
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">end</span> <span class="hljs-comment">//always end</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">function</span>  [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex_data; <span class="hljs-comment">//函数不含时序逻辑相关</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">input</span>   [<span class="hljs-number">03</span>:<span class="hljs-number">00</span>]	data_i;<span class="hljs-comment">//至少一个输入</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">begin</span>
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">case</span>(data_i)
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d0</span>:hex_data = NUM_0;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d1</span>:hex_data = NUM_1;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d2</span>:hex_data = NUM_2;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d3</span>:hex_data = NUM_3;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d4</span>:hex_data = NUM_4;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d5</span>:hex_data = NUM_5;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d6</span>:hex_data = NUM_6;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d7</span>:hex_data = NUM_7;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d8</span>:hex_data = NUM_8;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-number">'d9</span>:hex_data = NUM_9;
	<code-line class="line-numbers-rows"></code-line>				<span class="hljs-keyword">default</span>:hex_data = ALL_LIGHT;
	<code-line class="line-numbers-rows"></code-line>			<span class="hljs-keyword">endcase</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-keyword">end</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">endfunction</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">endmodule</span></pre></code-box>
	<p>HC_SR04_driver.v：</p>
	<code-box id="code-eFtQ3X"><button boxid="code-eFtQ3X" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-eFtQ3X pre" aria-label="复制代码" style="opacity: 0; visibility: hidden;"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-verilog hljs code-pre-line" highlighted="true" boxid="code-eFtQ3X" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">module</span> 	HC_SR04_TOP(
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>  			clk		,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>   		rstn	,
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">input</span>   		echo	, <span class="hljs-comment">// 距离信号</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span>   		trig	, <span class="hljs-comment">// 触发测距信号</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span> 	[<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex1	, <span class="hljs-comment">// -共阳极，低电平有效</span>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">output</span>  [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex2	, <span class="hljs-comment">// -</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>  [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex3	, <span class="hljs-comment">// -</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>  [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex4	, <span class="hljs-comment">//连接符</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>  [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex5	, <span class="hljs-comment">//cm -</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>  [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex6	, <span class="hljs-comment">//cm -</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>  [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex7	, <span class="hljs-comment">//cm -</span>
	<code-line class="line-numbers-rows"></code-line>    <span class="hljs-keyword">output</span>  [<span class="hljs-number">6</span>:<span class="hljs-number">0</span>]	hex8	  <span class="hljs-comment">//熄</span>
	<code-line class="line-numbers-rows"></code-line>);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span>	[<span class="hljs-number">18</span>:<span class="hljs-number">00</span>]		data_o		;
	<code-line class="line-numbers-rows"></code-line>	<span class="hljs-keyword">wire</span> 				clk_us		;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	seg_driver u_seg_driver(
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk</span>		(clk	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.rstn</span>		(rstn	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.data_in</span>	(data_o	), <span class="hljs-comment">//待显示数据</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.hex1</span>		(hex1	), <span class="hljs-comment">// -共阳极，低电平有效</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.hex2</span>		(hex2	), <span class="hljs-comment">// -</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.hex3</span>		(hex3	), <span class="hljs-comment">// -</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.hex4</span>		(hex4	), <span class="hljs-comment">//连接符</span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.hex5</span>		(hex5	), <span class="hljs-comment">//cm - </span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.hex6</span>		(hex6	), <span class="hljs-comment">//cm - </span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.hex7</span>		(hex7	), <span class="hljs-comment">//cm - </span>
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.hex8</span>		(hex8	)  <span class="hljs-comment">//熄灭</span>
	<code-line class="line-numbers-rows"></code-line>	);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	clk_div	u_clk_div(
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk</span>		(clk	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.rstn</span>		(rstn	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk_us</span>		(clk_us )
	<code-line class="line-numbers-rows"></code-line>	);
	<code-line class="line-numbers-rows"></code-line>	trig_driver	u_trig_driver(
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk_us</span>		(clk_us	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.rstn</span>		(rstn	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.trig</span>		(trig	)
	<code-line class="line-numbers-rows"></code-line>	);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	echo_driver	u_echo_driver(
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk</span>		(clk	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.clk_us</span>		(clk_us	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.rstn</span>		(rstn	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.echo</span>		(echo	),
	<code-line class="line-numbers-rows"></code-line>		<span class="hljs-variable">.data_o</span>		(data_o	)
	<code-line class="line-numbers-rows"></code-line>		);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">endmodule</span>
	<code-line class="line-numbers-rows"></code-line></pre></code-box>
	<h2 id="波形分析" tid="tid-xJEAQd">波形分析<button class="cnblogs-toc-button" title="显示目录导航" aria-expanded="false"></button></h2>
	<p>仿真文件设计如下：</p>
	<code-box id="code-A2D4FT"><button boxid="code-A2D4FT" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-A2D4FT pre" aria-label="复制代码"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-testbench code-pre-line" highlighted="true" boxid="code-A2D4FT" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>`timescale 1ns/1ns 		//仿真系统时间尺度定义
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>`define clk_period 20  	//时钟周期参数定义
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>module tb_hc_sr();
	<code-line class="line-numbers-rows"></code-line>//激励信号定义
	<code-line class="line-numbers-rows"></code-line>	reg				Clk		;
	<code-line class="line-numbers-rows"></code-line>	reg				Rst_n	;
	<code-line class="line-numbers-rows"></code-line>	reg				echo; //
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>//响应信号定义
	<code-line class="line-numbers-rows"></code-line>	wire	 		trig	;
	<code-line class="line-numbers-rows"></code-line>	wire	[6:0]	hex1	;
	<code-line class="line-numbers-rows"></code-line>	wire	[6:0]	hex2	;
	<code-line class="line-numbers-rows"></code-line>	wire	[6:0]	hex3	;
	<code-line class="line-numbers-rows"></code-line>	wire	[6:0]	hex4	;
	<code-line class="line-numbers-rows"></code-line>	wire	[6:0]	hex5	;
	<code-line class="line-numbers-rows"></code-line>	wire	[6:0]	hex6	;
	<code-line class="line-numbers-rows"></code-line>	wire	[6:0]	hex7	;
	<code-line class="line-numbers-rows"></code-line>	wire	[6:0]	hex8	;
	<code-line class="line-numbers-rows"></code-line>//实例化
	<code-line class="line-numbers-rows"></code-line>	HC_SR04_TOP		HC_SR04_TOP(
	<code-line class="line-numbers-rows"></code-line>		.Clk		(Clk		),
	<code-line class="line-numbers-rows"></code-line>		.Rst_n		(Rst_n		),
	<code-line class="line-numbers-rows"></code-line>		.echo		(echo		),
	<code-line class="line-numbers-rows"></code-line>		.trig		(trig		), //触发测距信号
	<code-line class="line-numbers-rows"></code-line>		.hex1		(hex1		), // -共阴极，低电平有效
	<code-line class="line-numbers-rows"></code-line>		.hex2		(hex2		), // -
	<code-line class="line-numbers-rows"></code-line>		.hex3		(hex3		), // -
	<code-line class="line-numbers-rows"></code-line>		.hex4		(hex4		), //连接符
	<code-line class="line-numbers-rows"></code-line>		.hex5		(hex5		), //cm -
	<code-line class="line-numbers-rows"></code-line>		.hex6		(hex6		), //cm -
	<code-line class="line-numbers-rows"></code-line>		.hex7		(hex7		), //cm -
	<code-line class="line-numbers-rows"></code-line>		.hex8		(hex8		)  //熄灭
	<code-line class="line-numbers-rows"></code-line>	);
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>	//产生时钟
	<code-line class="line-numbers-rows"></code-line>	initial Clk = 1'b0;
	<code-line class="line-numbers-rows"></code-line>	always #(`clk_period / 2) Clk = ~Clk;
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>//产生激励
	<code-line class="line-numbers-rows"></code-line>	initial  begin
	<code-line class="line-numbers-rows"></code-line>		Rst_n = 1'b0;
	<code-line class="line-numbers-rows"></code-line>		echo  = 1'b0;
	<code-line class="line-numbers-rows"></code-line>		#(`clk_period * 20 + 3);
	<code-line class="line-numbers-rows"></code-line>		Rst_n = 1'b1;
	<code-line class="line-numbers-rows"></code-line>		#(`clk_period * 20);
	<code-line class="line-numbers-rows"></code-line>		wait(HC_SR04_TOP.hc_sr_driver.hc_sr_trig.cnt == 240);
	<code-line class="line-numbers-rows"></code-line>		echo = 1'b1;//测试超声波信号发送完成，echo拉高
	<code-line class="line-numbers-rows"></code-line>		#(50 * `clk_period * 2500 + 7);
	<code-line class="line-numbers-rows"></code-line>		echo = 1'b0;
	<code-line class="line-numbers-rows"></code-line>		#(`clk_period * 200);
	<code-line class="line-numbers-rows"></code-line>		$stop(2);
	<code-line class="line-numbers-rows"></code-line>	end
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>endmodule</pre></code-box>
	<p>波形如下：</p>
	<div align="center">
	<a data-fancybox="gallery" href="https://img2023.cnblogs.com/blog/2829072/202305/2829072-20230521094533690-801346048.png"><img src="https://img2023.cnblogs.com/blog/2829072/202305/2829072-20230521094533690-801346048.png"></a>
	</div>
	<p>我们对 echo 设计的输入信号对应的距离为 021.250 cm，并设置 trig 输出 10μS 持续高电平，通过输出的位选信号，我们可以看到这就是我们需要的信号，所以可以判断我们的系统已经成功实现了所需要的功能</p>
	<h2 id="烧录验证" tid="tid-Ak6Rtc">烧录验证<button class="cnblogs-toc-button" title="显示目录导航" aria-expanded="false"></button></h2>
	<p>引脚配置文件如下：</p>
	<code-box id="code-ZW642K"><button boxid="code-ZW642K" type="button" class="clipboard code-copay-btn hljs-comment" data-clipboard-action="copy" data-clipboard-target="#code-ZW642K pre" aria-label="复制代码"><i class="iconfont icon-fuzhi1"></i></button><pre class="highlighter-hljs language-tcl hljs code-pre-line" highlighted="true" boxid="code-ZW642K" style="border-radius: 4px; max-height: 85vh; white-space: pre;"><code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># Copyright (C) 2018  Intel Corporation. All rights reserved.</span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># Your use of Intel Corporation's design tools, logic functions </span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># and other software and tools, and its AMPP partner logic </span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># functions, and any output files from any of the foregoing </span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># (including device programming or simulation files), and any </span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># associated documentation or information are expressly subject </span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># to the terms and conditions of the Intel Program License </span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># Subscription Agreement, the Intel Quartus Prime License Agreement,</span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># the Intel FPGA IP License Agreement, or other applicable license</span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># agreement, including, without limitation, that your use is for</span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># the sole purpose of programming logic devices manufactured by</span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># Intel and sold by Intel or its authorized distributors.  Please</span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># refer to the applicable agreement for further details.</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># Quartus Prime Version 18.1.0 Build 625 09/12/2018 SJ Standard Edition</span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># File: C:\Users\ppqpp\Desktop\FPGA-ultrasonic-ranging-main\HC_SR04\hc_sr04.tcl</span>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-comment"># Generated on: Sun May 21 09:32:17 2023</span>
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line><span class="hljs-keyword">package</span> require ::quartus::project
	<code-line class="line-numbers-rows"></code-line>
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_Y2 -to clk
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_M23 -to rstn
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_G18 -to hex1[<span class="hljs-number">0</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_F22 -to hex1[<span class="hljs-number">1</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_E17 -to hex1[<span class="hljs-number">2</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_L26 -to hex1[<span class="hljs-number">3</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_L25 -to hex1[<span class="hljs-number">4</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_J22 -to hex1[<span class="hljs-number">5</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_H22 -to hex1[<span class="hljs-number">6</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_M24 -to hex2[<span class="hljs-number">0</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_Y22 -to hex2[<span class="hljs-number">1</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_W21 -to hex2[<span class="hljs-number">2</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_W22 -to hex2[<span class="hljs-number">3</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_W25 -to hex2[<span class="hljs-number">4</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_U23 -to hex2[<span class="hljs-number">5</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_U24 -to hex2[<span class="hljs-number">6</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AA25 -to hex3[<span class="hljs-number">0</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AA26 -to hex3[<span class="hljs-number">1</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_Y25 -to hex3[<span class="hljs-number">2</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_W26 -to hex3[<span class="hljs-number">3</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_Y26 -to hex3[<span class="hljs-number">4</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_W27 -to hex3[<span class="hljs-number">5</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_W28 -to hex3[<span class="hljs-number">6</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_V21 -to hex4[<span class="hljs-number">0</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_U21 -to hex4[<span class="hljs-number">1</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AB20 -to hex4[<span class="hljs-number">2</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AA21 -to hex4[<span class="hljs-number">3</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AD24 -to hex4[<span class="hljs-number">4</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AF23 -to hex4[<span class="hljs-number">5</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_Y19 -to hex4[<span class="hljs-number">6</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AB19 -to hex5[<span class="hljs-number">0</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AA19 -to hex5[<span class="hljs-number">1</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AG21 -to hex5[<span class="hljs-number">2</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AH21 -to hex5[<span class="hljs-number">3</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AE19 -to hex5[<span class="hljs-number">4</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AF19 -to hex5[<span class="hljs-number">5</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AE18 -to hex5[<span class="hljs-number">6</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AD18 -to hex6[<span class="hljs-number">0</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AC18 -to hex6[<span class="hljs-number">1</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AB18 -to hex6[<span class="hljs-number">2</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AH19 -to hex6[<span class="hljs-number">3</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AG19 -to hex6[<span class="hljs-number">4</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AF18 -to hex6[<span class="hljs-number">5</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AH18 -to hex6[<span class="hljs-number">6</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AA17 -to hex7[<span class="hljs-number">0</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AB16 -to hex7[<span class="hljs-number">1</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AA16 -to hex7[<span class="hljs-number">2</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AB17 -to hex7[<span class="hljs-number">3</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AB15 -to hex7[<span class="hljs-number">4</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AA15 -to hex7[<span class="hljs-number">5</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AC17 -to hex7[<span class="hljs-number">6</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AD17 -to hex8[<span class="hljs-number">0</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AE17 -to hex8[<span class="hljs-number">1</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AG17 -to hex8[<span class="hljs-number">2</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AH17 -to hex8[<span class="hljs-number">3</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AF17 -to hex8[<span class="hljs-number">4</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AG18 -to hex8[<span class="hljs-number">5</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AA14 -to hex8[<span class="hljs-number">6</span>]
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AB22 -to trig
	<code-line class="line-numbers-rows"></code-line>set_location_assignment PIN_AC15 -to echo</pre></code-box>
	<div align="center">
	<a data-fancybox="gallery" href="https://img2023.cnblogs.com/blog/2829072/202305/2829072-20230520213200984-1948823782.gif"><img src="https://img2023.cnblogs.com/blog/2829072/202305/2829072-20230520213200984-1948823782.gif"></a>
	</div>
	<h2 id="参考资料" tid="tid-RzA2NX">参考资料<button class="cnblogs-toc-button" title="显示目录导航" aria-expanded="false"></button></h2>
	<p><a href="https://blog.csdn.net/qq_43546203/article/details/125281386" target="_blank" rel="noopener">基于DE2 115开发板驱动HC_SR04超声波测距模块【附源码】</a></p>
	
	<div> <nav id="articleDirectory" style="top: 286.275px; left: 5px; width: 260px; display: none;" class="articleDirectoryFixed"> <ul class="nav nav-pills"> <li class="nav-item"><a class="nav-link" href="#quartus-入门--fpga-超声波测距hc-sr04" goto="tid-p4ZBG2" onclick="return false;">Quartus 入门 —— FPGA 超声波测距（HC-SR04）</a></li><li class="nav-item"><a class="nav-link" href="#模块设计" goto="tid-wmnssx" onclick="return false;">&nbsp;&nbsp;&nbsp;&nbsp;模块设计</a></li><li class="nav-item"><a class="nav-link active" href="#代码设计" goto="tid-j3hkXm" onclick="return false;">&nbsp;&nbsp;&nbsp;&nbsp;代码设计</a></li><li class="nav-item"><a class="nav-link" href="#波形分析" goto="tid-xJEAQd" onclick="return false;">&nbsp;&nbsp;&nbsp;&nbsp;波形分析</a></li><li class="nav-item"><a class="nav-link" href="#烧录验证" goto="tid-Ak6Rtc" onclick="return false;">&nbsp;&nbsp;&nbsp;&nbsp;烧录验证</a></li><li class="nav-item"><a class="nav-link" href="#参考资料" goto="tid-RzA2NX" onclick="return false;">&nbsp;&nbsp;&nbsp;&nbsp;参考资料</a></li> </ul> </nav> </div><span class="articleSuffix-flg"> <br><p class="essaySuffix-eof">__EOF__</p> <div id="articleSuffix"> <div class="articleSuffix-bg"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 147.78 155.96"> <path d="M10.5,99.81a1.9,1.9,0,0,0-.53-.09,1.66,1.66,0,0,0-1.64,1.65A1.64,1.64,0,0,0,10,103a1.57,1.57,0,0,0,.87-.25l26.76,26.82.45-1.08L11.52,101.91A1.65,1.65,0,0,0,10.5,99.81Zm-.13,2a.57.57,0,0,1-.8,0,.58.58,0,0,1-.17-.41.58.58,0,0,1,.57-.57h0a.57.57,0,0,1,.56.58A.55.55,0,0,1,10.37,101.77Z" style="fill:#c5c9e0"></path><path d="M56.15,117.58H39.06l0-.09a1.65,1.65,0,0,0-1.36-1H37.5a1.65,1.65,0,1,0,1.56,2.19H55.7L92.92,156h41.44v-1.08h-41Zm-18.25.94a.56.56,0,0,1-.79,0,.58.58,0,0,1-.17-.41.57.57,0,0,1,.56-.57h0a.58.58,0,0,1,.57.58A.54.54,0,0,1,37.9,118.52Z" style="fill:#c5c9e0"></path><path d="M23.52,50.32a1.65,1.65,0,0,0,1.55-1.11H55.28l48-48.13h31.06V0H102.85l-48,48.13H25.07a1.64,1.64,0,0,0-2.09-1,1.64,1.64,0,0,0,.54,3.2Zm0-2.21a.57.57,0,0,1,0,1.13.57.57,0,1,1,0-1.13Z" style="fill:#c5c9e0"></path><polygon points="102.86 0 102.86 0 102.86 0 102.86 0" style="fill:#c5c9e0"></polygon><path d="M107.72,12.14h26.64V11.07H107.27L57.4,61H3.09a1.66,1.66,0,0,0-1.45-.86H1.52A1.65,1.65,0,1,0,2.81,63a1.59,1.59,0,0,0,.45-.87H57.85ZM2.05,62.23a.57.57,0,0,1-.8,0,.58.58,0,0,1-.17-.41.57.57,0,0,1,.56-.57h.09a.57.57,0,0,1,.32,1Z" style="fill:#c5c9e0"></path><path d="M134.36,43.22V42.14h-22.3l-9.62,9.63a1.64,1.64,0,0,0-2.19.77,1.61,1.61,0,0,0-.17.71,1.65,1.65,0,1,0,3.29,0,1.61,1.61,0,0,0-.16-.72l9.3-9.32Zm-32.64,10.6a.57.57,0,0,1,0-1.13.57.57,0,0,1,0,1.13Z" style="fill:#c5c9e0"></path><path d="M147,52.3l-9,9H111.48a1.64,1.64,0,0,0-1.61-1.33h-.14a1.65,1.65,0,1,0,1.6,2.41h27.19l9.26-9.29L147,52.3Zm-37.15,9.85a.56.56,0,0,1-.56-.57h0a.56.56,0,0,1,.56-.56h0a.57.57,0,1,1,0,1.13Z" style="fill:#c5c9e0"></path><path d="M66.79,75.35l11,11.06h56.53V85.33H78.27l-11-11.06H49.49L37.12,86.67a1.64,1.64,0,0,0-2.09,1,1.61,1.61,0,0,0-.09.54,1.65,1.65,0,0,0,3.29,0,1.68,1.68,0,0,0-.26-.89l12-12ZM36.58,88.79a.57.57,0,1,1,.57-.56A.57.57,0,0,1,36.58,88.79Z" style="fill:#c5c9e0"></path><path d="M110.61,95.55,92.8,113.4a1.62,1.62,0,1,0,.77.76l17.49-17.53h23.31V95.55ZM92.49,115.28a.56.56,0,0,1-.8,0,.58.58,0,0,1-.17-.41.57.57,0,0,1,.57-.57h0a.58.58,0,0,1,.56.58A.55.55,0,0,1,92.49,115.28Z" style="fill:#c5c9e0"></path><path d="M97.89,122.3H76.62L64.2,109.85a1.65,1.65,0,0,0-.77-2.2,1.77,1.77,0,0,0-.72-.17h-.14a1.65,1.65,0,0,0,.15,3.29,1.58,1.58,0,0,0,.71-.17l12.74,12.77H98.34l17.48-17.52h18.54v-1.08h-19ZM63.12,109.53a.56.56,0,0,1-.8,0,.58.58,0,0,1-.17-.41.57.57,0,0,1,1.14,0A.54.54,0,0,1,63.12,109.53Z" style="fill:#c5c9e0"></path> </svg> </div> <div class="articleSuffix-left"> <a data-fancybox="gallery" href="https://pic.cnblogs.com/avatar/2829072/20220404213906.png"><img src="https://pic.cnblogs.com/avatar/2829072/20220404213906.png"></a> </div> <div class="articleSuffix-right"> <item> <li> <b>本文作者：</b> <a href="https://www.cnblogs.com/ppqppl" target="_blank">ppqppl</a> </li> <li> <b>本文链接：</b> <a href="https://www.cnblogs.com/ppqppl/articles/17417550.html" target="_blank">https://www.cnblogs.com/ppqppl/articles/17417550.html</a> </li> <li> <b>关于博主：</b> 评论和私信会在第一时间回复。或者<a href="https://msg.cnblogs.com/msg/send/ppqppl" target="_blank">直接私信</a>我。 </li> <li> <b>版权声明：</b> 本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-nd/4.0/" alt="BY-NC-SA" title="BY-NC-SA" target="_blank">BY-NC-SA</a> 许可协议。转载请注明出处！ </li> <li> <b>声援博主：</b> 如果您觉得文章对您有帮助，可以点击文章右下角<strong><span style="color: #ff0000; font-size: 12pt;">【<a id="post-up" onclick="votePost(17417550,'Digg')" href="javascript:void(0);">推荐</a>】</span></strong>一下。 </li> </item> </div> <div style="clear:both"></div> </div> </span></div>